define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'choi/user/index' + location.search,
                    add_url: 'choi/user/add',
                    edit_url: 'choi/user/edit',
                    del_url: 'choi/user/del',
                    multi_url: 'choi/user/multi',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'user_id',
                sortName: 'user_id',
                search: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'user_id', title: __('User_id')},
                        // {field: 'username', title: __('Username')},
                        {field: 'nickname', title: __('Nickname'), operate: 'LIKE'},
                        // {field: 'password', title: __('Password')},
                        // {field: 'salt', title: __('Salt')},
                        // {field: 'email', title: __('Email')},
                        {field: 'mobile', title: __('Mobile')},
                        {field: 'avatar', title: __('Avatar'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        // {field: 'level', title: __('Level')},
                        // {field: 'gender', title: __('Gender')},
                        // {field: 'birthday', title: __('Birthday'), operate:'RANGE', addclass:'datetimerange'},
                        {field: 'money', title: __('Money'), operate:'BETWEEN', table: table, events: Table.api.events.operate, formatter: function (e, data) {
                            return `<a href="javascript:;" class="btn btn-xs btn-primary btn-money" data-id="`+data.user_id+`" title="删除">`+data.money+`</a>`;
                        }},
                        // {field: 'score', title: __('Score')},
                        // {field: 'successions', title: __('Successions')},
                        // {field: 'maxsuccessions', title: __('Maxsuccessions')},
                        // {field: 'prevtime', title: __('Prevtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'logintime', title: __('Logintime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'loginip', title: __('Loginip')},
                        // {field: 'loginfailure', title: __('Loginfailure')},
                        // {field: 'joinip', title: __('Joinip')},
                        // {field: 'jointime', title: __('Jointime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'token', title: __('Token')},
                        // {field: 'status', title: __('Status'), formatter: Table.api.formatter.status},
                        // {field: 'is_freezeswitch', title: __('Is_freezeswitch'), table: table, formatter: Table.api.formatter.toggle},
                        {field: 'wxapp_openid', title: __('Wxapp_openid'), operate: false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate: false, addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: function (e, data) {
                                return  ` <a href="javascript:;" class="btn btn-xs btn-primary btn-recharge" data-id="`+data.user_id+`" title="删除">充值记录</a> ` +
                                    ` <a href="javascript:;" class="btn btn-xs btn-success btn-editone" title="编辑"><i class="fa fa-pencil"></i></a> ` +
                                    ` <a href="javascript:;" class="btn btn-xs btn-danger btn-delone" title="删除"><i class="fa fa-trash"></i></a> ` ;
                            }
                        }
                    ]
                ]
            });

            //账号余额明细
            table.on('click', '.btn-money', function () {
                var userId = $(this).data('id');
                Fast.api.open('/keyu.php/choi/user_money_log/log?user_id='+userId, '金额变动明细');
            })

            //充值记录
            table.on('click', '.btn-recharge', function () {
                var userId = $(this).data('id');
                Fast.api.open('/keyu.php/choi/user_recharge_log/log?user_id='+userId, '充值记录');
            })


            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'choi/user/recyclebin' + location.search,
                pk: 'user_id',
                sortName: 'user_id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'user_id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'choi/user/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'choi/user/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});